package Simulator.MdtGUI;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

import Simulator.MDT;

public class MDTScreen extends JFrame {

	
	private JPanel top;
	private MDT mdt;
	private MDTGUILoggedPanel loggedPanel;
	private MDTGUILoginPanel loginPanel;
	private JPanel centerPanel;
	private MDTScreen ref;
	private Thread loggedPanelThread;
	public MDTScreen(final MDT mdt)
	{
		this.ref = this;
		this.mdt = mdt;
		this.setTitle("Ambulance MDT");
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); // Voir le WindowListener, un peu plus bas dans le constructeur
		this.setSize(350, 400);
        this.setLocationRelativeTo(null);
		
		// Pour mettre une icone perso a la place de l'icone Java
		//Image icone = Toolkit.getDefaultToolkit().getImage("images/icone.png");
		//this.setIconImage(icone);
		
		final Container container = this.getContentPane();
		container.setLayout(new BorderLayout());
		
		top = new MDTGUITopPanel();
		top.setPreferredSize(new Dimension(350,50));
		
	    loggedPanel = new MDTGUILoggedPanel(this.mdt,this);
	    loggedPanel.setMaximumSize(new Dimension(350,300));
	    
	    loginPanel = new MDTGUILoginPanel(this.mdt,this);
	    loginPanel.setMaximumSize(new Dimension(350,300));
		
	    centerPanel = new JPanel();
	    centerPanel.setLayout(new CardLayout());
	    
	    centerPanel.add("loginPanel",loginPanel);
	    centerPanel.add("loggedPanel",loggedPanel);

	    if(mdt!=null)
	    {
	    	if(mdt.isLogged())
	    		((CardLayout) centerPanel.getLayout()).show(centerPanel,"loggedPanel");
	        else
	        {
	    		((CardLayout) centerPanel.getLayout()).show(centerPanel,"loginPanel");
	        }
	    }
	    
		container.add(top, BorderLayout.NORTH);
		container.add(centerPanel, BorderLayout.CENTER);
		
		// on met le loggedPanel dans un thread parce qu'il execute en parallele une methode qui met a jour les infos dans le gui tous les x ms
		loggedPanelThread = new Thread(loggedPanel);
        loggedPanelThread.start();
        
		this.addWindowListener(new java.awt.event.WindowAdapter() {
	        public void windowClosing(java.awt.event.WindowEvent evt) {
	        	mdt.setGUIUndisplayed();
	        	loggedPanel.stopLoop();
	            ref.dispose();
	        } 
		} );
		
		setResizable(false);
        setVisible(true);
	}
	
	public void setLoginPanel()
	{
		((CardLayout) this.centerPanel.getLayout()).show(this.centerPanel, "loginPanel");
	}
	public void setLoggedPanel()
	{
		((CardLayout) this.centerPanel.getLayout()).show(this.centerPanel, "loggedPanel");
		
		if(mdt!=null)
			if(mdt.isLogged())
				this.loggedPanel.refreshData(-1);

	}
	
	public void refreshButtons()
	{
		loginPanel.refreshButtons();
	}

}
